############################
#### REPLICATION MASTER ####
############################

### This master script follows the structure and coding of the Master script
### in the replication dataverse files provided by Skorge (2021)

#### SETUP ####

if (!require("pacman")) {
  install.packages("pacman")
  library(pacman)
}
# Note by Skorge (2021): pacman function "p_load installs packages that are not already
# installed and loads all packages. 
# Please make sure that all packages are up to date (add option 
# update=TRUE to p_load to update)
p_load(here, data.table, tidyverse, readxl, plyr, haven, foreign, 
       knitr, bookdown, car, zoo, rlist, estimatr, kableExtra)


# Make sure that here() function is from "here" package
here <- here::here


# Make sure to set parent directory, i.e., the
# folder in which all the new replication files are located:
# Change to the correct folder on your computer. (Windows
# users may have to replace backslashes \ with double backslashes \\)

# Run:
setwd("C:/Users/Mona_Morgan-Collins/replication_skorge2021_review") # change to correct folder path
here::i_am("replication_master.r") # to set correct path
here() # check that gives correct path

# Load the user-defined functions in utils.r - This script is the original one 
# provided by Skorge (2021) without any modification
source(here("utils","utils.r"))

#### CREATE MAIN ANALYSIS DATA: DATA ON NORWEGIAN MUNICIPALITIES ####

# Puts the data together from the source data. This script is the original 
# one provided by Skorge (2021) without any modification
source(here("data","data_main_01_data_from_sources.r"))


# Defines and creates all variables used in the analyses. This script is an
# adaptation of the original code provided by Skorge (2021), slighlty modified to
# include the gender turnout gap
source(here("data_set.r"))

# Creates the table with the replications of the original analyses. This is a new
# script created using Skorge (2021) analyses code
source(here("replication_originals.r"))

# Creates the table with the replications of the analyses using the 
# alternative specification of the dependent variable (i.e., Gender Turnout Gap).
# This is script, as the previous one, is an adaptation of Skorge (2021) code
# modified to run the anlayses with the new dependent variable
source(here("replication_alternative_dv.r"))

## After running this code, the parent directory will now have a folder called 
## "Tables" including both the table with the replication of the original models
## and the table with the new dependent variable.
